package com.jacob.cloud.mall.categoryproduct.controller;

import com.github.pagehelper.PageInfo;
import com.jacob.cloud.mall.categoryproduct.model.pojo.Category;
import com.jacob.cloud.mall.categoryproduct.model.request.AddCategoryReq;
import com.jacob.cloud.mall.categoryproduct.model.request.UpdateCategoryReq;
import com.jacob.cloud.mall.categoryproduct.model.vo.CategoryVO;
import com.jacob.cloud.mall.categoryproduct.service.ICategoryService;
import com.jacob.cloud.mall.common.common.ApiRestResponse;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 分类目录模块
 */
@RestController
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    /**
     * 增加目录分类
     */
    @ApiOperation("增加目录分类")
    @PostMapping("/admin/category/add")
    public ApiRestResponse<Object> addCategory(@Valid @RequestBody AddCategoryReq addCategoryReq) {
        categoryService.add(addCategoryReq);
        return ApiRestResponse.success();
    }

    /**
     * 更新目录分类
     */
    @ApiOperation("更新目录分类")
    @PostMapping("/admin/category/update")
    public ApiRestResponse<Object> updateCategory(@Valid @RequestBody UpdateCategoryReq updateCategoryReq) {
        categoryService.update(updateCategoryReq);
        return ApiRestResponse.success();
    }

    /**
     * 删除分类
     */
    @ApiOperation("删除分类 ")
    @PostMapping("/admin/category/delete")
    public ApiRestResponse<Object> deleteCategory(@RequestParam("id") Integer categoryId) {
        categoryService.delete(categoryId);
        return ApiRestResponse.success();
    }

    /**
     * 后台分类列表
     */
    @ApiOperation("后台分类列表（平铺） ")
    @GetMapping("/admin/category/list")
    public ApiRestResponse<Object> listCategoryForAdmin(@RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        PageInfo<Category> categoryPageInfo = categoryService.listCategory(pageNum, pageSize);
        return ApiRestResponse.success(categoryPageInfo);
    }

    /**
     * 前台分类列表
     */
    @ApiOperation("前台分类列表（递归） ")
    @GetMapping("/category/list")
    public ApiRestResponse<Object> listCategoryForCustomer() {
        List<CategoryVO> categoryVOS = categoryService.listCategoryForCustomer(0);
        return ApiRestResponse.success(categoryVOS);
    }
}
